<?php

class UserCategory extends GxActiveRecord
{
	/**
	 * The followings are the available columns in table 'profiles':
	 * @var integer $user_id
	 * @var boolean $regMode
	 */
	public $regMode = false;
	
	private $_model;
	private $_modelReg;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		//return Yii::app()->getModule('user')->tableProfiles;
		return 'user_categories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('id, category_id, user_id', 'safe'),    
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => UserModule::t('Id'),
			'user_id' => UserModule::t('User'),
			'category_id' => UserModule::t('Category'),
		);
	}
	
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		//$criteria->compare('category_id', $this->name, true);
		//$criteria->compare('user_id', $this->alias, true);
		$criteria->order = 'id ASC';
		$criteria->compare('tbl_users.username', $this->user, true, 'OR');
		$criteria->with = array('user');
		$criteria->compare('categories.name', $this->category, true, 'OR');
		$criteria->with = array('category');

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}
	public function behaviors() {
		return array(
			'AutoTimestampBehavior' => array(
				'class' => 'application.components.AutoTimestampBehavior',
				//You can optionally set the field name options here
			)
		);
	}
}